package daa.levelup.aCipher;


public class aVizener implements ICipher {
    String alphabet;

    public aVizener(String _alphabet) {
        alphabet = _alphabet;
    }


    @Override
    public String getEncryption(String text, String key_str) {
        StringBuilder ans = new StringBuilder();
        try {
            char[] chars = text.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                int openCharNum = alphabet.indexOf(chars[i]);
                int keyNum = alphabet.indexOf(key_str.charAt(i % key_str.length()));
                int encCharNum = (openCharNum + keyNum) % alphabet.length();
                char encChar = alphabet.charAt(encCharNum);
                ans.append(encChar);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ans.toString();
    }


    @Override
    public String getDecryption(String cipher, String key_str) {
        StringBuilder ans = new StringBuilder();
        try {
        char[] chars = cipher.toCharArray();
        for (int i = 0; i < cipher.length(); i++) {
            int openCharNum = alphabet.indexOf(chars[i]);
            int keyNum = alphabet.indexOf(key_str.charAt(i % key_str.length()));
            int decCharNum = (openCharNum - (keyNum - alphabet.length())) % alphabet.length();
            char decChar = alphabet.charAt(decCharNum);

            ans.append(decChar);
        }
        }catch (Exception e){
                e.printStackTrace();
               }
         return ans.toString();
    }
}
